package com.zasaps.player;

//import java.io.File;

import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;



public class zasaps extends Activity {
	
	static final int DIALOG_ADD_SUB = 0;
	static final int DIALOG_DELETE_QUEUE = 1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        
        //firstRun();
        
        // uncommenting this breaks the app... don't know why the listview won't populate
        populateQueue();
        
        
        
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)  {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.play_menu, menu);
    	return true;
    	
    }
    
    protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        switch(id) {
        case DIALOG_ADD_SUB:
            // do the work to define the pause Dialog
        	
        	AlertDialog.Builder builder;
        	AlertDialog alertDialog;

        	Context mContext = getApplicationContext();
        	LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        	View layout = inflater.inflate(R.layout.add_sub_layout,
        	                               (ViewGroup) findViewById(R.id.layout_add_sub));

        	TextView text = (TextView) layout.findViewById(R.id.text_add);
        	text.setText("RSS feed:");
        	

        	builder = new AlertDialog.Builder(mContext);
        	builder.setView(layout);
        	alertDialog = builder.create();
        	dialog = alertDialog;
            break;
        //case DIALOG_DELETE_QUEUE:
            // do the work to define the game over Dialog
          //  break;
        default:
            dialog = null;
        }
        return dialog;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)  {
    	// handle item selection
    	switch (item.getItemId())  {
    	case R.id.add_sub:
    		//onCreateDialog(DIALOG_ADD_SUB);
    		Intent myIntent = new Intent(zasaps.this, AddSubscription.class);
    		zasaps.this.startActivity(myIntent);
    		return true;
    	case R.id.quit:
    		finish();
    		return true;
    	default:
    		return super.onOptionsItemSelected(item);
    	}
    }
    
    private void populateQueue() {
    	// for now we will populate the queue based on any audio fiels in the dir
    	// later we will read a db to order them
    	
    	
    		// create adapter for listview
    		 
    		String[] myFiles = new String[5]; // myDir.list();
    			myFiles[0] = "test 1";
    			myFiles[1] = "test 2";
    			myFiles[2] = "test 3 ";
    			myFiles[3] = "test 4";
    			myFiles[4] = "test 5";
    			
        		Intent myIntent = new Intent(this, QueueViewActivity.class);
        		startActivity(myIntent);
    			
    		//ArrayAdapter<String> a_adapter = new ArrayAdapter<String>(this.getBaseContext(), R.id.queueview);
    		
    		//for( String myString : myFiles)
    		//{
    		//	a_adapter.add(myString);
    		//}

    		//ListView lv = (ListView)findViewById(R.id.queueview);
    		//lv.setAdapter(a_adapter);

    
    }
    
    private void firstRun() {
    
    	// check for path
    	boolean mExternalStorageAvailable = false;
    	boolean mExternalStorageWriteable = false;
    	String state = Environment.getExternalStorageState();

    	if (Environment.MEDIA_MOUNTED.equals(state)) {
    	    // We can read and write the media
    	    mExternalStorageAvailable = mExternalStorageWriteable = true;
    	    // create directory if it doesn't exist
    	    
    	    //File myDir = Environment.getExternalStorageDirectory(R.string.storage_dir);
    	    
    	} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    	    // We can only read the media
    	    mExternalStorageAvailable = true;
    	    mExternalStorageWriteable = false;
    	} else {
    	    // Something else is wrong. It may be one of many other states, but all we need
    	    //  to know is we can neither read nor write
    	    mExternalStorageAvailable = mExternalStorageWriteable = false;
    	}
    	
    	
    	
    
    }
    /*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.play_menu, menu);
        return true;
    } */
    
   /*
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.quit:
            //quit();
        	finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    } */
}